package com.example.home.active_function;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.tool.GeneralClass;
import com.example.home.tool.OKHttpTool;

import org.json.JSONException;
import org.json.JSONObject;

import static com.example.home.tool.StaticActi.ip;

public class ListParticulars extends AppCompatActivity {
    private String newListId;
    private ListParticularsBean listBean;
    private TextView categoryName;
    private TextView apply;
    private ImageView imgUrl;
    private TextView name;
    private TextView content;
    private TextView tv_look_comment;
    private TextView tv_number;
    private RecyclerView activity;
    private RecyclerView rcv_comment;
    private EditText ed_import;
    private TextView tv_comment;
    //private boolean isAlreadyRegistered = false; // 添加一个标志来表示用户是否已报名
    private boolean isAlreadyRegistered = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_particulars);
        Intent intent = getIntent();
        newListId = intent.getStringExtra("newlist_id");
        initView();
        //获取头部数据
        getDataBean();
        //活动推荐
        getRecommend();
        addcomment(5);
        Registrationactivity();
    }

    private void Registrationactivity() {
        apply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 在点击按钮之前检查是否已经报名
                if (isAlreadyRegistered) {
                    Toast.makeText(ListParticulars.this, "您已经报名成功！", Toast.LENGTH_SHORT).show();
                    return; // 不执行报名操作
                }

                // 调用isAlreadyRegistered方法检查用户是否已经报名
                isAlreadyRegistered(newListId);
            }
        });
    }

    private void isAlreadyRegistered(String actId) {
        OKHttpTool.doNetRequest("prod-api/api/activity/signup/check/" + actId,
                OKHttpTool.GET,
                GeneralClass.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        GeneralClass generalClass = (GeneralClass) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (generalClass.isIsSignup()) {
                                    Toast.makeText(ListParticulars.this, "您已经报名成功！", Toast.LENGTH_SHORT).show();
                                } else {
                                    // 如果用户尚未报名，执行报名操作
                                    performRegistration();
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {
                        // 在请求失败时，可以考虑允许用户再次尝试报名
                    }
                });
    }

    private void performRegistration() {
        // 执行报名操作，和之前的代码相同
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("activityId", String.valueOf(newListId));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        OKHttpTool.doNetRequest("prod-api/api/activity/signup",
                OKHttpTool.POST,
                GeneralClass.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        GeneralClass generalClass = (GeneralClass) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (generalClass.getCode() == 200) {
                                    Toast.makeText(ListParticulars.this, "报名成功", Toast.LENGTH_SHORT).show();
                                    // 标记为已经报名成功
                                    isAlreadyRegistered = true;
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {
                        // 在请求失败时，可以考虑允许用户再次尝试报名
                    }
                }, jsonObject.toString()
        );
    }


    private void addcomment(Integer id) {
        tv_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String CommentContent = ed_import.getText().toString();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("activityId",String.valueOf(id));
                    jsonObject.put("content",CommentContent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                OKHttpTool.doNetRequest("prod-api/api/activity/comment",
                        OKHttpTool.POST,
                        ListParticularsBean.class,
                        new OKHttpTool.NetCallback() {
                            @Override
                            public void onSuccess(Object obj) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Log.e("xhf","访问成功");
                                        ListParticularsBean bean = (ListParticularsBean)obj;
                                        if(bean.getCode()==200){
                                            //重新获取评论信息
                                            //getreview(id);
                                            getCommendList(id);
                                            Toast.makeText(ListParticulars.this.getApplicationContext(),"发表评论成功",Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }

                            @Override
                            public void onFailure(String msg) {
                                Log.e("xhf","访问失败");
                            }
                        },jsonObject.toString()
                );
            }
        });
    }

    private void getCommendList(Integer ID) {
        OKHttpTool.doNetRequest("prod-api/api/activity/comment/list?activityId="+ID+"&pageNum=1&pageSize=8",
                OKHttpTool.GET,
                CommendListBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        CommendListBean commendListBean = (CommendListBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindCommendList(commendListBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindCommendList(CommendListBean commendListBean) {
        RecyclerView recyclerView = findViewById(R.id.rcv_comment);
        CommendListAdapter commendListAdapter = new CommendListAdapter(ListParticulars.this,commendListBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ListParticulars.this);
        recyclerView.setAdapter(commendListAdapter);
        recyclerView.setLayoutManager(linearLayoutManager);
    }

    private void getRecommend() {
        OKHttpTool.doNetRequest("prod-api/api/activity/activity/list",
                OKHttpTool.GET,
                ActivityBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        ActivityBean activityBean = (ActivityBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindList(activityBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {
                        Log.e("xhf","访问失败");
                    }
                }
        );
    }

    private void bindList(ActivityBean activityBean) {
        RecyclerView recyclerView =findViewById(R.id.activity);
        ActivityAdapter activityAdapter = new ActivityAdapter(ListParticulars.this,activityBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ListParticulars.this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(activityAdapter);
    }

    private void getDataBean() {
        OKHttpTool.doNetRequest("prod-api/api/activity/activity/" + newListId,
                OKHttpTool.GET,
                ListParticularsBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        ListParticularsBean listBean = (ListParticularsBean) obj;
                        runOnUiThread(new Runnable() {
                            @RequiresApi(api = Build.VERSION_CODES.N)
                            @Override
                            public void run() {
                                Glide.with(ListParticulars.this).load(ip+listBean.getData().getImgUrl()).into(imgUrl);
                                categoryName.setText(listBean.getData().getCategoryName());
                                name.setText(listBean.getData().getName());
                                String htmlText = listBean.getData().getContent(); // 用你的HTML文本替换这里的内容
                                CharSequence sequence = Html.fromHtml(htmlText, Html.FROM_HTML_MODE_COMPACT);
                                content.setText(sequence);

                                //评论详细
                                getCommendList(listBean.getData().getId());
                                tv_look_comment.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        //根据id获得评论条数
                                        getreview(listBean.getData().getId());
                                        //发表评论
                                        addcomment(listBean.getData().getId());

                                    }
                                });

                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void getreview(Integer ID) {
        OKHttpTool.doNetRequest("prod-api/api/activity/comment/number?activityId=" + ID,
                OKHttpTool.GET,
                GeneralClass.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        runOnUiThread(new Runnable() {
                            GeneralClass generalClass = (GeneralClass) obj;
                            @Override
                            public void run() {
                                tv_number.setVisibility(View.VISIBLE);
                                tv_number.setText("当前有"+String.valueOf(generalClass.getCommentNum())+"条评论");
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void initView() {
        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ListParticulars.this.finish();
            }
        });
        categoryName = findViewById(R.id.categoryName);
        apply = findViewById(R.id.apply);
        imgUrl = findViewById(R.id.imgUrl);
        name = findViewById(R.id.name);
        content = findViewById(R.id.content);
        tv_look_comment = findViewById(R.id.tv_look_comment);
        tv_number = findViewById(R.id.tv_number);
        activity = findViewById(R.id.activity);
        rcv_comment = findViewById(R.id.rcv_comment);
        ed_import = findViewById(R.id.ed_import);
        tv_comment = findViewById(R.id.tv_comment);
    }

}